#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Andevilin
# 功能：实现迷宫绘制的功能

from cell import U_WALL, D_WALL, L_WALL, R_WALL

# 颜色变量
BGC = '#ff0ff0ff0'# 白色
FGC = '#000000000'# 黑色
WKC = '#000fc0fc0'# 湖蓝
VSC = '#0000000cf'# 黑色
RRR = '#fff000000'# 红色
GGG = '#000fff000'# 绿色
BBB = '#000000fff'# 蓝色
XXX = '#0ff0ff000'# 黑色
LGB = '#8f08f0fff'# 浅蓝



# 迷宫最小格子的大小
CELL_WIDTH = 40
CELL_HEIGHT = 40

# 迷宫与窗口顶部的距离
MARGIN_TOP = 40
# 迷宫与窗口左侧的距离
MARGIN_LEFT = 40

class Graphics(object):
    # 定义DEBUG变量，初始值为0
    DEBUG = 0
    # 定义cellheight变量，值为CELL_HEIGHT
    cellheight = CELL_HEIGHT
    # 定义cellwidth变量，值为CELL_WIDTH
    cellwidth = CELL_WIDTH
    # 定义walker变量，初始值为(0, 0), walker代表在迷宫中行走的小点
    walker = (0, 0)
    # 定义mz变量，初始值为空列表
    mz = []

    class Cell(object):
        # 定义cell类，用于表示迷宫中的格子,实现每个小格子的绘制功能
        def __init__(self, field, pos, width, height):
            self.field = field
            self.pos = pos
            x0,y0 = pos # 获取格子的左上角坐标
            self.ld = (x0 + width, y0) # 右上角坐标
            self.ru = (x0, y0 + height) # 左下角坐标
            self.rd = (x0 + width, y0 + height) # 右下角坐标
            self.center = (x0 + width//2, y0 + height//2) # 格子中心坐标

            # 障碍墙
            x1,y1 = self.ru  # 获取格子的右上角坐标
            x2,y2 = self.rd  # 获取格子的右下角坐标
            x3,y3 = self.ld  # 获取格子的左下角坐标
            # 绘制格子的上边线，即上面墙
            self.uw = field.create_line(y0,x0,y1,x1,fill = FGC, disabledfill = BGC)
            # 绘制格子的右边线，即右边墙
            self.rw = field.create_line(y1,x1,y2,x2,fill = FGC, disabledfill = BGC)
            # 绘制格子的下边线，即下面墙
            self.dw = field.create_line(y2,x2,y3,x3,fill = FGC, disabledfill = BGC)
            # 绘制格子的左边线，即左边墙
            self.lw = field.create_line(y3,x3,y0,x0,fill = FGC, disabledfill = BGC)
            # print(type(self.lw))

            # 绘制走迷宫的小点
            x0 += 2
            y0 += 2
            x2 -= 2
            y2 -= 2
            self.player = field.create_oval(y0, x0, y2, x2, fill = BGC, disabledfill = BGC, outline = BGC)
            # print(self.walker)
            # print(type(self.walker))

        # 定义clear方法，用于清除格子的墙
        def clear(self):
            self.field.itemconfigure(self.uw, fill = FGC) # FGC为黑色
            self.field.itemconfigure(self.rw, fill = FGC)
            self.field.itemconfigure(self.dw, fill = FGC)
            self.field.itemconfigure(self.lw, fill = FGC)
            self.field.itemconfigure(self.player, fill = BGC)  # BGC为白色

        # 清除小点的颜色
        def clearWalker(self):
            self.field.itemconfigure(self.player, fill = BGC)  # BGC 为白色

        # 设置小点的颜色
        def setWalker(self):
            self.field.itemconfigure(self.player, fill = BBB)  # BBB 为蓝色

        # 标记为浅蓝色
        def markWalker(self):
            self.field.itemconfigure(self.player, fill = LGB)  # LGB 为浅蓝色

        # 设置看答案部分的小点颜色为绿色
        def setAnswerWalker(self):
            self.field.itemconfigure(self.player, fill = GGG)  # GGG 为绿色

        # 设置看答案部分的小点颜色为浅蓝色
        def markAnswerWalker(self):
            self.field.itemconfigure(self.player, fill = LGB)  # LGB 为浅蓝色

        # 标记为黑色
        def markVisited(self):
            self.field.itemconfigure(self.player, fill = VSC) # VSC 为黑色

        # 标记格子的颜色
        def markCell(self, color):
            self.field.itemconfigure(self.player, fill = color)

        # 清除格子的颜色，即去掉对应的边（墙）
        def breakWall(self, wall):
            if wall == U_WALL:
                self.field.itemconfigure(self.uw, fill = BGC)  # BGC 为白色
            elif wall == R_WALL:
                self.field.itemconfigure(self.rw, fill = BGC)  # BGC 为白色
            elif wall == D_WALL:
                self.field.itemconfigure(self.dw, fill = BGC)  # BGC 为白色
            elif wall == L_WALL:
                self.field.itemconfigure(self.lw, fill = BGC)  # BGC 为白色


    def __init__(self, field, height, width):
        self.width = width
        self.height = height
        self.field = field
        # 生成迷宫格子
        for i in range(0, height):
            self.mz.append([])
            for j in range(0, width):
                pos = (i * self.cellheight + MARGIN_LEFT, j * self.cellwidth + MARGIN_TOP)
                cel = self.Cell(field, pos, self.cellwidth, self.cellheight)
                self.mz[i].append(cel)

    # 重置迷宫，实现迷宫的初始化
    def clear(self):
        # 获取walker的坐标
        x,y = self.walker
        # 清除walker
        self.mz[x][y].clearWalker()
        # 将walker的坐标设置为(0,0)
        self.walker = (0,0)
        # 遍历地图中的每个格子
        for i in range(0, self.height):
            for j in range(0, self.width):
                # 清除格子中的内容
                self.mz[i][j].clear()

    # 生成入口和出口
    def breakWall(self, x, y, wall):
        if wall == 'U':
            self.mz[x][y].breakWall(U_WALL)
        else:
            self.mz[x][y].breakWall(D_WALL)

    # 打破两个格子之间的墙，形成连接通道
    def connectCells(self, x, y, x1, y1):
        # 如果x等于x1
        # print("test")
        if x == x1:
            # 如果y小于y1
            if y < y1:
                # 如果DEBUG不等于0
                if self.DEBUG != 0:
                    # 标记格子(x,y)和(x1,y1)为黑色
                    self.mz[x][y].markCell(VSC)
                    self.mz[x][y1].markCell(VSC)
                    # 打印连接格子信息
                    print("连接格子:(", x,",", y,") R_WALL 和 （" ,x1,",", y1,") L_WALL")
                # 打破格子(x,y)的R_WALL
                self.mz[x][y].breakWall(R_WALL)
                # 打破格子(x1,y1)的L_WALL
                self.mz[x1][y1].breakWall(L_WALL)
            # 否则
            else:
                # 如果DEBUG不等于0
                if self.DEBUG != 0:
                    # 标记格子(x,y)的左边和(x1,y1)的右边为黑色
                    self.mz[x][y].markCell(VSC)
                    self.mz[x1][y1].markCell(VSC)
                    # 打印连接格子信息
                    print("连接格子:(", x, ",", y, ") L_WALL 和 （", x1, ",", y1, ") R_WALL")
                # 打破格子(x,y)的L_WALL
                self.mz[x][y].breakWall(L_WALL)
                # 打破格子(x1,y1)的R_WALL
                self.mz[x1][y1].breakWall(R_WALL)
        # 否则
        else:
            # 如果x小于x1
            if x < x1:
                # 如果DEBUG不等于0
                if self.DEBUG != 0:
                    # 标记格子(x,y)和(x1,y1)为黑色
                    self.mz[x][y].markCell(VSC)
                    self.mz[x1][y1].markCell(VSC)
                    # 打印连接格子信息
                    print("连接格子:(", x, ",", y, ") D_WALL 和 （", x1, ",", y1, ") U_WALL")
                # 打破格子(x,y)的D_WALL
                self.mz[x][y].breakWall(D_WALL)
                # 打破格子(x1,y1)的U_WALL
                self.mz[x1][y1].breakWall(U_WALL)
            # 否则
            else:
                # 如果DEBUG不等于0
                if self.DEBUG != 0:
                    # 标记格子(x,y)和(x1,y1)为黑色
                    self.mz[x][y].markCell(VSC)
                    self.mz[x1][y1].markCell(VSC)
                    # 打印连接格子信息
                    print("连接格子:(", x, ",", y, ") U_WALL 和 （", x1, ",", y1, ") D_WALL")
                # 打破格子(x,y)的U_WALL
                self.mz[x][y].breakWall(U_WALL)
                # 打破格子(x1,y1)的D_WALL
                self.mz[x1][y1].breakWall(D_WALL)


    # 清除每个格子中的移动痕迹
    def clearWalker(self, i, j ):
        self.mz[i][j].clearWalker()

    # 使得出口处的更加明显
    def setGoal(self, i, j):
        self.mz[i][j].markCell(RRR)

    # 移动小点从一个格子到另一个格子
    def setWalker(self, i, j):
        x,y = self.walker
        self.mz[x][y].clearWalker()
        self.mz[i][j].setWalker()
        self.walker = (i,j)

    # 显示小点的移动轨迹
    def moveWalker(self, i, j):
        x, y = self.walker
        self.mz[x][y].markWalker()
        self.mz[i][j].setWalker()
        self.walker = (i,j)

    # 显示答案小点的移动轨迹
    def moveWalkerAnswer(self, i, j):
        x, y = self.walker
        self.mz[x][y].markAnswerWalker()
        self.mz[i][j].setAnswerWalker()
        self.walker = (i,j)